package com.qitmiaojie.controller;

import com.qitmiaojie.controller.utils.Result;
import com.qitmiaojie.pojo.Temperature;
import com.qitmiaojie.service.TemperatureService;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@RestController
@Slf4j
public class TemperatureController {
    @Autowired
    private TemperatureService temperatureService;

    @Value("${photo.path}")
    private String photoPath;
    @PostMapping("/addTemperature")
    public Result addTemperature(@RequestBody Temperature temperature){
        log.info(temperature.toString());
        return temperatureService.addTemperature(temperature);
    }
    @GetMapping("/getAllIsTemperature")
    public Result getAllIsTemperature(String date){
        log.info(date);
        return temperatureService.getAllIsTemperature(date);
    }
    @GetMapping("/getTemperature/{today}")
    public Result getTemperature(@PathVariable String today){
        return temperatureService.getTemperature(today);
    }
    @PostMapping("/upload")
    public Result journey(@RequestBody MultipartFile file) throws Exception {
        log.info(file.toString());
        String filename = file.getOriginalFilename();
        int i = filename.lastIndexOf(".");
        String end = filename.substring(i);
        String name = UUID.randomUUID().toString()+end;
        file.transferTo(new File(photoPath+name));
        return Result.success(name);
    }
    @GetMapping("/download")
    public void download(String name, HttpServletResponse response){
        log.info("name = {}",name);
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(new File(photoPath+name));
            log.info(fis.toString());
            ServletOutputStream sos = response.getOutputStream();
            response.setContentType("image/jpeg");
            IOUtils.copy(fis,sos);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

/*
*             date:this.searchForm.date,
                        name:this.searchForm.name,
                        status:this.searchForm.status,
                        idNumber:this.searchForm.IDNumber,
                        result:this.searchForm.TestingResult,
                        currentPage:this.page.CurrentPage,
                        size:this.page.Size,*/
    @GetMapping("/temperatureAndHealthCode")
    public Result getAlltemperatureAndHealthCode(@RequestParam(name = "date",required = false) String date,
                                                 @RequestParam(name = "name",required = false) String name,
                                                 @RequestParam(name = "status",required = false) String status,
                                                 @RequestParam(name = "idNumber",required = false) String idNumber,
                                                 @RequestParam(name = "result",required = false) String result,
                                                 @RequestParam(name = "currentPage",required = false)Integer currentPage,
                                                 @RequestParam(name = "size",required = false)Integer size) throws IOException, TencentCloudSDKException, InterruptedException {
        log.info("{},{},{},{},{},{},{},{}",date,name,status,idNumber,result,currentPage,size);
        return temperatureService.getAlltemperatureAndHealthCode(date,name,status,idNumber,result,currentPage,size);
    }
}
